package main

import (
	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"log"
	"loveserver/config"
	"loveserver/model"
	"loveserver/router"
)

// * 读取命令行配置（config文件）
var cfg = pflag.StringP("config", "c", "", "apiserver config file path.")

// @title A love application API
// @version 1.0
// @description This is a love application server.
// @BasePath /api
func main() {

	// * 初始化配置
	pflag.Parse()
	if err := config.Init(*cfg); err != nil {
		panic(err)
	}
	log.Printf("The config inited.")


	// * 初始化数据库
	model.DB.Init()
	defer model.DB.Close()
	log.Printf("The database inited.")

	// * 设置 run mode
	gin.SetMode(viper.GetString("runmode"))
	log.Printf("Gin run mode set to: %s", viper.GetString("runmode"))

	// * gin engine.
	g := gin.New()

	router.Load(g)
	log.Printf("The gin engine started, and the router loaded.")

	log.Printf("Start to listening on: %s", viper.GetString("addr"))
	g.Run(viper.GetString("addr")).Error()
}
